package com.yundows.safe;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.yundows.safe.pick.Pick;

public class StartActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startactivity);
		new Handler(new Callback() {				
			@Override
			public boolean handleMessage(Message arg0) {
				SharedPreferences guideornot = getSharedPreferences("guide", Activity.MODE_PRIVATE);
				if(guideornot.getBoolean("flag", true)){
					addContentView(LayoutInflater.from(getBaseContext()).inflate(R.layout.guide, null), new FrameLayout.LayoutParams(-1, -1));
					// TODO Auto-generated method stub
					new Handler(new Callback() {				
						@Override
						public boolean handleMessage(Message arg0) {
							View v = findViewById(R.id.guideview);
							v.setVisibility(View.GONE);
							return false;
						}
					}).sendEmptyMessageDelayed(0, 5000);
					SharedPreferences.Editor editor = guideornot.edit();
			        editor.putBoolean("flag", false);
			        editor.commit();
				}
				
				return false;
			}
		}).sendEmptyMessageDelayed(0, 1000);			
			
		//startActivity(new Intent(StartActivity.this,ServiceActivity.class));
		Intent i = new Intent(StartActivity.this, ProtectService.class);
        //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startService(i);
		
		
		Button exerciseButton = (Button) findViewById(R.id.exerciseButton);			
		exerciseButton.setOnClickListener(new Button.OnClickListener()
	    {
	    	public void onClick(View v)
	    	{
	    		Intent intent = new Intent();
	    		intent.setClass(StartActivity.this, AuthActivity.class).putExtra("training", true);	    		
	    		startActivity(intent);
	    	}
	    });
		
//		Button passwordSettingButton = (Button) findViewById(R.id.passwordSettingButton);
//		passwordSettingButton.setOnClickListener(new Button.OnClickListener()
//	    {
//	    	public void onClick(View v)
//	    	{
////	    		Intent intent = new Intent();
////	    		intent.setClass(StartActivity.this, LogListener.class);
////	    		
////	    		startActivity(intent);
//	    	}
//	    });
//		
//    	Button testButton = (Button) findViewById(R.id.testButton);
//    	testButton.setOnClickListener(new Button.OnClickListener()
//	    {
//	    	public void onClick(View v)
//	    	{
////	    		Intent intent = new Intent();
////	    		intent.setClass(StartActivity.this, .class);
////	    		
////	    		startActivity(intent);
//	    	}
//	    });
		
		Button selectButton = (Button) findViewById(R.id.selectButton);
		selectButton.setOnClickListener(new Button.OnClickListener()
	    {
	    	public void onClick(View v)
	    	{
	    		Intent intent = new Intent();
	    		intent.setClass(StartActivity.this, Pick.class);	    		
	    		startActivity(intent);
	    	}
	    });
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Button exerciseButton = (Button) findViewById(R.id.exerciseButton);	
		SharedPreferences  sp = getSharedPreferences("trained", Activity.MODE_PRIVATE);
		if(sp.getBoolean("trained", false))
			exerciseButton.setBackgroundResource(R.drawable.avatar_true);
		else
			exerciseButton.setBackgroundResource(R.drawable.avatar_false);
	}
	
	public static void setTrained(Context context, boolean trained) {
		SharedPreferences  sp = context.getSharedPreferences("trained", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.putBoolean("trained", trained);
		editor.commit();
	}
 
}
